DESTDIR:=
LANGS:=$(basename $(wildcard *.po))

.PHONY: all pot install clean uninstall

all: build

# ---

habr-monitor-applet.pot: ../src/*.py
	if which pygettext &>/dev/null; \
		then PYGETTEXT=pygettext; \
	elif which pygettext.py &>/dev/null; \
		then PYGETTEXT=pygettext.py; \
	elif which xgettext &>/dev/null; \
		then PYGETTEXT='xgettext --language=Python'; \
	else \
		echo 'pygettext not found'; \
		exit 1; \
	fi; \
	TEMP=`mktemp` && \
	$$PYGETTEXT -o $$TEMP $^ && \
	sed 's/CHARSET/UTF-8/' $$TEMP | \
		sed 's/ENCODING/UTF-8/' > $@
pot: habr-monitor-applet.pot

%.po: habr-monitor-applet.pot
	TEMP=`mktemp` && msgmerge $@ $^ > $$TEMP && mv $$TEMP $@

%.mo: %.po
	msgfmt -o $@ $^

# ---

build: $(addsuffix .mo, $(LANGS))

install-%: %.mo
	DIR="$(DESTDIR)"/usr/share/locale/$*/LC_MESSAGES; \
	mkdir -p -- "$${DIR}" && cp -- $^ "$${DIR}"/habr-monitor-applet.mo
install: $(LANGS:%=install-%)

clean:
	-rm habr-monitor-applet.pot *.mo

uninstall-%:
	-rm -- "$(DESTDIR)"/usr/share/locale/$*/LC_MESSAGES/habr-monitor-applet.mo
uninstall: $(LANGS:%=uninstall-%)
